package com.leenleda.ips.util;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import com.leenleda.ips.BaseActivity;
import com.leenleda.ips.entity.BaseEntity;
import com.leenleda.ips.receiver.Constant;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class BaseUtils {

    public static boolean isJson(String content) {
        try {
            if (content.contains("[") && content.contains("]")) {
                new org.json.JSONArray(content);
                return true;
            } else {
                new org.json.JSONObject(content);
                return true;
            }
        } catch (org.json.JSONException e) {
            return false;
        }
    }


    private static String tag = "BaseUtils";

    public static BaseEntity getBase(){
        String deviceInfo = "";
        BaseEntity info = null;
        deviceInfo = UShared.getString(Constant.PUBLISH_BASE);//从shredPreference 中获取历史记录
        LogUtils.e(tag, "-----deviceInfo--" + deviceInfo);
        try {
            if (deviceInfo.isEmpty()){
                info = getFileInfo();
            } else {
                if (isJson(deviceInfo)) {
                    info = GsonUtils.getInstance().fromJson(deviceInfo, BaseEntity.class);
                } else {
                    UShared.putString(Constant.PUBLISH_BASE, "");
                    info = getFileInfo();
                }
            }
            if (info != null)
                return info;
        } catch (Exception e){
            UShared.putString(Constant.PUBLISH_BASE, "");
        }
        return new BaseEntity();
    }

    /**
     * 获取deviceInfo
     */
    public static BaseEntity getFileInfo(){
        String deviceInfo = "";
        String path = getCachePath(BaseActivity.getForegroundActivity()) + Constant.PUBLISH_FILE_BASE;
        File file = new File(path);
        BaseEntity info = null;
        LogUtils.e(tag, "====getFileInfo=exists===" + file.exists() );
        if (!file.exists())
            return null;
        try {
            InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
            BufferedReader br = new BufferedReader(isr);

            String mimeTypeLine = null ;
            while ((mimeTypeLine = br.readLine()) != null) {
                deviceInfo = deviceInfo+mimeTypeLine;
            }
            isr.close();
            br.close();
            if (deviceInfo.isEmpty())
                return null;
            if (isJson(deviceInfo)){
                UShared.putString(Constant.PUBLISH_BASE, deviceInfo);
                info = GsonUtils.getInstance().fromJson(deviceInfo, BaseEntity.class);
            }
            if (info == null)
                return null;
//            UShared.putString(Constant.PUBLISH_BASE, deviceInfo);
            LogUtils.e(tag, "====FileInfo=2===" + info );
            return info;
        } catch (IOException e) {
            e.printStackTrace();
        }
        UShared.putString(Constant.PUBLISH_BASE, "");
        file.delete();
        return null;
    }

    /**
     * 删除/保存info
     * @param info
     */
    public static boolean setDeviceInfo(BaseEntity info){
        String deviceInfo = GsonUtils.getInstance().toJson(info);
        if (deviceInfo.isEmpty())
            return false;
//        FileUtils.setServeIp(info.getServiceIp());
        UShared.putString(Constant.PUBLISH_BASE, deviceInfo);
        LogUtils.e(tag, "===setDeviceInfo===1=" + deviceInfo );
        String path = getCachePath(BaseActivity.getForegroundActivity()) + Constant.PUBLISH_FILE_BASE;
        LogUtils.e(tag, "====FileInfo=2===" + deviceInfo );
        try {
            FileWriter writer = new FileWriter(path);
            writer.write(deviceInfo);
            writer.flush();
            writer.close();
            LogUtils.e(tag, "====setDeviceInfo===2=" );
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            LogUtils.e(tag, "====setDeviceInfo===23=" + e);
        }
        return false;
    }

    private static String getCachePath(Context context) {
        String path = null;
        try {
            path = Environment.getExternalStorageDirectory().getCanonicalPath() + "/LELDIPS/";
            File file = new File(path);
            if (!file.exists()) {
                file.mkdirs();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.i("--getCachePath--", "getCrashFilePath: " + path);
        return path;
    }

    public static void clearService(){
        UShared.putString(Constant.PUBLISH_TYPE, "");
        UShared.putBooleanValue(Constant.DEVICE_REGISTER, false);
        UShared.putString(Constant.SET_INPUT_IP, "");

        UShared.putString(Constant.PUBLISH_IP, "");
        String pathIp = getCachePath(BaseActivity.getForegroundActivity()) + Constant.PUBLISH_FILE_IP;
        File fileIp = new File(pathIp);
        if (fileIp.exists())
            fileIp.delete();

        UShared.putString(Constant.PUBLISH_BASE, "");
        String pathBase = getCachePath(BaseActivity.getForegroundActivity()) + Constant.PUBLISH_FILE_BASE;
        File fileBase = new File(pathBase);
        if (fileBase.exists())
            fileBase.delete();

        UShared.putString(Constant.PUBLISH_DATA, "");
        String pathData = getCachePath(BaseActivity.getForegroundActivity()) + Constant.PUBLISH_FILE_DATA;
        File fileData = new File(pathData);
        if (fileData.exists())
            fileData.delete();
    }
}
